package com.base.myproject.client.HR.Books;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.tools.FileDialog;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.TriggerField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * 图书维护
 * @author Administrator
 *
 */
//图书状态有3种：借出，已归还，锁定；是否损坏有1种：损坏

public class Set_books extends BaseJDBCPanel{
	Button to_ok = new Button ("已修复");
	Button to_lock = new Button ("锁定");
	Button re_lock = new Button ("取消锁定");
	Button bt = new Button ("查询");
	LabelField lab = new LabelField("状态");
	StoreFilterField<BusiObject> ajaxfield;//类似ajar的自动查询框
	PagingToolBar toolBar1 = null;
	BaseComboBoxX status = new BaseComboBoxX("name","dictionary","where type ='bookStatus'");
	TextField<String> tx = new TextField<String> ();  
	
	public Set_books(){
		init();
	}
	public void init(){
		initwidget();
		registerMainStore(store);
		registerStore(store);
	}
	public void complete()
	{
		super.complete();
		getToolbarll().add(to_ok);
		getToolbarll().add(to_lock);
		getToolbarll().add(re_lock);
		getToolbarll().add(new LabelField("快速查找"));
		getToolbarll().add(tx);
		getToolbarll().add(lab);
		getToolbarll().add(status);
		getToolbarll().add(bt);
	}
	private void initwidget() {
		toolBar1 = new PagingToolBar(30);
		
		to_ok.addListener(Events.OnClick, getListener());
		to_lock.addListener(Events.OnClick, getListener());
		re_lock.addListener(Events.OnClick, getListener());
		bt.addListener(Events.OnClick, getListener());
		
		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.setBodyBorder(false);
		panel2.add(getgrid());
		panel2.setBottomComponent(toolBar1);
		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		vp.add(panel2);
		
		add(vp);
		
	}
	
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}
	
	ModelType type;
	BaseStore<BusiObject> createStore() {
		type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bookNo");
		type.getField("bookNo").setType(String.class);
		type.addField("sort");
		type.getField("sort").setType(String.class);
		type.addField("bookName");
		type.getField("bookName").setType(String.class);
		type.addField("library");
		type.getField("library").setType(String.class);
		type.addField("address");
		type.getField("address").setType(String.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		type.addField("isbreak");
		type.getField("isbreak").setType(String.class);
		type.addField("addDate");
		type.getField("addDate").setType(Date.class);
		type.addField("returnDate");
		type.getField("returnDate").setType(Date.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_book");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_book order by addDate asc");
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}
/**********************************/	
	BaseGrid<BaseObject> grid = null;
	BaseComboBoxX bcbStyle;
	
	BaseGrid<BaseObject> getgrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}
	
	DateField df;
	BaseGrid<BaseObject> createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C2 = new ColumnConfig("bookNo", "条形码", 120);
		TextField<String> t1 = new TextField<String>();
		C2.setEditor(new CellEditor(t1));
		columns.add(C2);
		ColumnConfig C4 = new ColumnConfig("bookName", "图书名", 160);
		TextField<String> t3 = new TextField<String>();
		C4.setEditor(new CellEditor(t3));
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("library", "所在馆", 80);
		TextField<String> t4 = new TextField<String>();
		C5.setEditor(new CellEditor(t4));
		columns.add(C5);
		ColumnConfig C9 = new ColumnConfig("address", "藏馆地", 100);
		TextField<String> t5 = new TextField<String>();
		C9.setEditor(new CellEditor(t5));
		columns.add(C9);
		ColumnConfig C6 = new ColumnConfig("status", "状态", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("isbreak", "是否损坏", 80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("addDate", "借入日期", 100);
		C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		df = new DateField();
		df.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C8.setEditor(new CellEditor(df));
		columns.add(C8);
		ColumnConfig C10 = new ColumnConfig("returnDate", "规定还书日期", 100);
		C10.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C10);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BaseObject>(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.setHeight(500);
		grid.setWidth(1000);
		grid.setSelectionModel(cbs);
		grid.addListener(Events.BeforeEdit, getListener());
		grid.getStore().reload(); // 暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {

						}  else if (be.getType() == Events.OnClick) {
							List<BaseObject> mList=grid.getSelectionModel().getSelectedItems();
							if (be.getSource().equals(to_ok)) {
								for (BaseObject m : mList) {
									int index= grid.getStore().indexOf(m);
									grid.setValue(index, "isbreak", "");
								}
							}
							if (be.getSource().equals(to_lock)) {
								for (BaseObject m : mList) {
									int index= grid.getStore().indexOf(m);
									grid.setValue(index, "status", "锁定");
								}
							}
							if (be.getSource().equals(re_lock)) {
								for (BaseObject m : mList) {
									int index= grid.getStore().indexOf(m);
									grid.setValue(index, "status", "正常");
								}
							}
							
							if(be.getSource().equals(bt)){
								String sql = " select * from hr_book where 1=1 ";
								 
								if (status.getRawValue().equals("")&&tx.getRawValue().equals("")) {
									MessageBox.alert("提示", "请输入查询条件，谢谢！", null);
									return;
								}
								if (!tx.getRawValue().equals("")) {
									sql += " and bookNo like'%25" + tx.getRawValue() + "%25' or bookName like '%25" + tx.getRawValue() + "%25'";
									}
								if (!status.getRawValue().equals("")) {
								sql += " and status = '" + status.getValue().get("value") + "'";
								}
								grid.getStore().getStoreinfo().setSql(sql);
								grid.getStore().reload();
							}
						} else if (be.getType() == Events.BeforeEdit) {
							final GridEvent<BusiObject> be2 = (GridEvent<BusiObject>)be;
							if (be2.getProperty().equals("addDate")) {
								if (store.getValue("addDate") != null) {
									//df.setEnabled(false);
									df.setReadOnly(true);
									Info.display("提示", "借入日期已经有值了，不能再修改");
								} else {
									df.setReadOnly(false);
								}
							}
						}
					}
					
					} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}	
	/**
	 * 过滤
	 * @param store
	 * @param parent
	 * @param record
	 * @param property
	 * @param filter
	 * @return  ajax 查询
	 */
	protected boolean Select(Store<BusiObject> store,
			BusiObject parent, BusiObject record, String property,
			String filter) {
		for (int i = 0; i < type.getFieldCount(); i++) {
			Object name = record.get(type.getField(i).getName());	
			//TODO
			if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
				return true;
			}
		}
		return false;
	}	
	StoreFilterField cread_field(){
		ajaxfield = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
	
			return	Select(store,parent,record,property,filter);
			}
	
			@Override
			protected void onFilter() {
				super.onFilter();
	
			}
	
		};
		ajaxfield.setAutoWidth(true);
		ajaxfield.setToolTip("书库快速查找");
		ajaxfield.bind(getStore());
		this.ajaxfield=ajaxfield;
		return ajaxfield;
	}
	/************************************/
}
